package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.CuccPosServiceUtils;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

public class SimCardTypeAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    private IOperateSceneInstService operateSceneInstService;
    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        String cateId =  CuccPosServiceUtils.getCateId(data.getSceneInstId(),data.getCustId());
        if (KiteStringUtils.equals(cateId,"2004072")){
            //雁飞5G 智连专线产品 连接不用此逻辑
            return;
        }
        if (KiteStringUtils.equals("2826", data.getServiceOfferId())) {
            //企业自采
            KiteAttr enterPriseSelfPurchaseAttr = inst.getAttr("200003676");
            if (Objects.nonNull(enterPriseSelfPurchaseAttr)) {
                //企业自采选择值为【是】,卡商要展示
                if ("1_0000106".equals(enterPriseSelfPurchaseAttr.getValue())) {
                    LocalAttrHelper.showAndNonnull(inst, "200004040");
                } else if ("1_0000107".equals(enterPriseSelfPurchaseAttr.getValue())){
                    if (!KiteStringUtils.equals("100007000", attr.getValue())) {
                        LocalAttrHelper.hideAndNullable(inst, "200004040");
                    } else {
                        LocalAttrHelper.showAndNonnull(inst, "200004040");
                    }
                }
            }
            KiteAttr simCardEntitySpec = inst.getAttr("200004041");
            KiteAttr presetElectronicCard = inst.getAttr("200004042");
            KiteAttr eachPhysicalCard = inst.getAttr("200004043");
            if (KiteStringUtils.equalsAny(attr.getValue(), "100007000")) {
                simCardEntitySpec.setShowType("D");
                simCardEntitySpec.setIsNull("F");
                presetElectronicCard.setShowType("D");
                presetElectronicCard.setIsNull("F");
            } else {
                simCardEntitySpec.setShowType("E");
                simCardEntitySpec.setIsNull("T");
                presetElectronicCard.setShowType("E");
                presetElectronicCard.setIsNull("T");
            }
            if (KiteStringUtils.equalsAny(attr.getValue(), "100007000", "100007001")) {
                eachPhysicalCard.setShowType("D");
                eachPhysicalCard.setIsNull("F");
            } else {
                eachPhysicalCard.setShowType("E");
                eachPhysicalCard.setIsNull("T");
            }
        }
    }

    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        super.after(attr, inst, data);
        String cateId =  CuccPosServiceUtils.getCateId(data.getSceneInstId(),data.getCustId());
        if (KiteStringUtils.equals(cateId,"2004072")){
            //雁飞5G 智连专线产品连接 不用此逻辑
            return;
        }
        //产品变更 需置灰
        if (KiteStringUtils.isEqual(data.getServiceOfferId(), "2826")
            && KiteStringUtils.equals(inst.getSpecId(), ProdCateUtil.getMainMemberProdId(ProdCateUtil.GIFT_DZ5G_PROD))) {
            attr.setIsEdit(KeyConsts.IFTRUE_F);
            operateSceneInstService
                .setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr);
        }
    }
}
